package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationPictureAfterView;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationPictureBeforeView;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationQuestionFeedBackView;
import com.cloudsigns.factorymanager2.engineering.IView.IUploadPhotoView;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationPictureAfterPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationPictureBeforePresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationQuestionFeedBackPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.UploadPhotoPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;
import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by tony on 2018/5/30.
 */

public class AddPublicationFeedBackActivity extends Activity implements View.OnClickListener, BottomListDialog.OnItemDialogClick, IUploadPhotoView, IPublicationPictureBeforeView, IPublicationPictureAfterView, IPublicationQuestionFeedBackView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private RadioGroup rg_feed_back_type;
    private RadioButton rb_befor_publication_feedback;
    private View rl_befor_publication, rl_afterr_publication, ll_wenti_feedback;
    private ImageView imgbefor_publication, img_after_publication;
    private EditText edt_wentineirong;
    private ImageView img_xianchang_picture;
    private TextView btn_submit;
    private final int BEFORE_FEEDBACK = 0;
    private final int AFTER_FEEDBACK = 1;
    private final int WENTI_FEEDBACK = 2;
    private int feedBackType = BEFORE_FEEDBACK;
    private String filePath;
    private MediaBean mediaBean;
    private PublicationOrderDetailBean publicationOrderDetailBean;
    private UploadPhotoPresenter uploadPhotoPresenter;
    private PublicationPictureBeforePresenter publicationPictureBeforePresenter;
    private PublicationPictureAfterPresenter publicationPictureAfterPresenter;
    private PublicationQuestionFeedBackPresenter publicationQuestionFeedBackPresenter;
    List<String> beforePicList = new ArrayList<>();
    List<String> afterPicList = new ArrayList<>();
    List<String> wentiPicList = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_add_publication_feedback);

        publicationPictureBeforePresenter = new PublicationPictureBeforePresenter(this);
        publicationPictureAfterPresenter = new PublicationPictureAfterPresenter(this);
        publicationQuestionFeedBackPresenter = new PublicationQuestionFeedBackPresenter(this);
        uploadPhotoPresenter = new UploadPhotoPresenter(this);
        init();

        tv_title.setText("上刊反馈");
        updateUI();

        if (null != getIntent()) {
            publicationOrderDetailBean = (PublicationOrderDetailBean) getIntent().getSerializableExtra(ConstantUtil.KEY_1);
            mediaBean = (MediaBean) getIntent().getSerializableExtra(ConstantUtil.KEY_2);
        }

    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);

        rg_feed_back_type = findViewById(R.id.rg_feed_back_type);
        rb_befor_publication_feedback = findViewById(R.id.rb_befor_publication_feedback);

        rl_befor_publication = findViewById(R.id.rl_befor_publication);
        rl_afterr_publication = findViewById(R.id.rl_afterr_publication);
        ll_wenti_feedback = findViewById(R.id.ll_wenti_feedback);

        imgbefor_publication = findViewById(R.id.imgbefor_publication);

        img_after_publication = findViewById(R.id.img_after_publication);

        edt_wentineirong = findViewById(R.id.edt_wentineirong);
        img_xianchang_picture = findViewById(R.id.img_xianchang_picture);

        btn_submit = findViewById(R.id.btn_submit);

        rl_back.setOnClickListener(this);

        imgbefor_publication.setOnClickListener(this);
        img_after_publication.setOnClickListener(this);
        img_xianchang_picture.setOnClickListener(this);

        btn_submit.setOnClickListener(this);

        rb_befor_publication_feedback.setChecked(true);
        rg_feed_back_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
                if (id == R.id.rb_befor_publication_feedback) {
                    feedBackType = BEFORE_FEEDBACK;
                    updateUI();

                } else if (id == R.id.rb_completion_feedback) {
                    feedBackType = AFTER_FEEDBACK;
                    updateUI();
                } else if (id == R.id.rb_wenti_feedback) {
                    feedBackType = WENTI_FEEDBACK;
                    updateUI();
                }
            }
        });

    }


    private void updateUI() {
        if (feedBackType == BEFORE_FEEDBACK) {
            rl_befor_publication.setVisibility(View.VISIBLE);
            rl_afterr_publication.setVisibility(View.GONE);
            ll_wenti_feedback.setVisibility(View.GONE);
        } else if (feedBackType == AFTER_FEEDBACK) {
            rl_befor_publication.setVisibility(View.GONE);
            rl_afterr_publication.setVisibility(View.VISIBLE);
            ll_wenti_feedback.setVisibility(View.GONE);
        } else if (feedBackType == WENTI_FEEDBACK) {
            rl_befor_publication.setVisibility(View.GONE);
            rl_afterr_publication.setVisibility(View.GONE);
            ll_wenti_feedback.setVisibility(View.VISIBLE);
        }
    }


    private void showListDialog() {
        DialogItemBean[] items = new DialogItemBean[]{
                new DialogItemBean(R.string.camera,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.gallary,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.cancel,
                        R.layout.dialog_list_item_normal)};
        BottomListDialog dialog = new BottomListDialog(this, Arrays.asList(items));
        dialog.setItemClick(this);
        dialog.show();
    }


    private void submit() {
        if (feedBackType == BEFORE_FEEDBACK) {
            publicationPictureBeforePresenter.publicationPictureBefore();
        } else if (feedBackType == AFTER_FEEDBACK) {
            publicationPictureAfterPresenter.publicationPictureAfter();
        } else if (feedBackType == WENTI_FEEDBACK) {
            publicationQuestionFeedBackPresenter.publicationQuestionFeedBack();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.imgbefor_publication:
                showListDialog();
                break;
            case R.id.img_after_publication:
                showListDialog();
                break;
            case R.id.img_xianchang_picture:
                showListDialog();
                break;
            case R.id.btn_submit:
                submit();
                break;
        }
    }


    @Override
    public void onItemDialogClick(DialogItemBean item) {
        switch (item.getmTextId()) {
            case R.string.camera: {

                PhotoUtil.startCamear(AddPublicationFeedBackActivity.this, null);
                break;
            }
            case R.string.gallary: {
                PhotoUtil.startGallery(AddPublicationFeedBackActivity.this, null);
                break;
            }
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {

            switch (requestCode) {

                case PhotoUtil.CAMERA:
                    if (resultCode == RESULT_OK) {
                        File picture = new File(PhotoUtil.filePath);
                        if (!picture.exists()) {
                            picture = new File(PhotoUtil.filePath);
                        }

                        filePath = PhotoUtil.CAMERA_SEVER + picture.getName();
                        filePath = PhotoUtil.imageCompressionAndSave(picture.getAbsolutePath(), filePath);
                        //override(100, 100)

                        if (feedBackType == BEFORE_FEEDBACK) {
                            Glide.with(this).load(picture).asBitmap().into(imgbefor_publication);
                        } else if (feedBackType == AFTER_FEEDBACK) {
                            Glide.with(this).load(picture).asBitmap().into(img_after_publication);

                        } else if (feedBackType == WENTI_FEEDBACK) {
                            Glide.with(this).load(picture).asBitmap().into(img_xianchang_picture);

                        }

                        uploadPhotoPresenter.uploadPhtoto();

                    }

                    break;

                case PhotoUtil.GALLERY:
                    if (resultCode == RESULT_OK) {
                        if (feedBackType == BEFORE_FEEDBACK) {
                            Glide.with(this).load(data.getData()).asBitmap().into(imgbefor_publication);

                        } else if (feedBackType == AFTER_FEEDBACK) {
                            Glide.with(this).load(data.getData()).asBitmap().into(img_after_publication);
                        } else if (feedBackType == WENTI_FEEDBACK) {

                        }
                        Glide.with(this).load(data.getData()).asBitmap().into(img_xianchang_picture);

                        String path = FileUtil.getRealPathFromURI(this, Uri.parse(data.getData() + ""));
                        File f = new File(path);
                        filePath = PhotoUtil.CAMERA_SEVER + f.getName();
                        filePath = PhotoUtil.imageCompressionAndSave(path, filePath);
                        uploadPhotoPresenter.uploadPhtoto();

                    }
                    break;

            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getEngineeringPublicationMediaId() {
        if (null != mediaBean)
            return mediaBean.getId();
        return "";
    }

    @Override
    public String getOrderId() {
        if (null != publicationOrderDetailBean)
            return publicationOrderDetailBean.getId();
        return null;
    }

    @Override
    public List<String> getFeedbackPicList() {
        return wentiPicList;
    }

    @Override
    public String getUnusualReason() {
        return edt_wentineirong.getText().toString().trim();
    }

    @Override
    public void publicationQuestionFeedBackSuccess() {
        Toast.makeText(this, "问题反馈成功", Toast.LENGTH_SHORT).show();
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());
        finish();
    }

    @Override
    public void publicationQuestionFeedBackFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public List<String> getPicList() {
        if (feedBackType == BEFORE_FEEDBACK) {
            return beforePicList;
        } else if (feedBackType == AFTER_FEEDBACK) {
            return afterPicList;
        } else if (feedBackType == WENTI_FEEDBACK) {
            return wentiPicList;
        }

        return null;
    }

    @Override
    public void publicationPictureAfterSuccess() {
        Toast.makeText(this, "完成反馈成功", Toast.LENGTH_SHORT).show();
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());
        finish();
    }

    @Override
    public void publicationPictureAfterFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void publicationPictureBeforeSuccess() {

        Toast.makeText(this, "上刊前反馈成功", Toast.LENGTH_SHORT).show();
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());

//        Intent mIntent = new Intent();
//        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void publicationPictureBeforeFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getFilePath() {
        return filePath;
    }


    @Override
    public void upLoadPhotoSuccessed(String uri) {
        if (feedBackType == BEFORE_FEEDBACK) {
            beforePicList.clear();
            beforePicList.add(uri);
        } else if (feedBackType == AFTER_FEEDBACK) {
            afterPicList.clear();
            afterPicList.add(uri);
        } else if (feedBackType == WENTI_FEEDBACK) {
            wentiPicList.clear();
            wentiPicList.add(uri);
        }

    }

    @Override
    public void upLoadPhotoFailed(String desc) {
//        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}
